


class MyArray2<T> {
    Object[] objects = new Object[10];
    //T[] objects = (T[]) new Object[10];

    public void setObjects(int pos, T value) {
        objects[pos] = value;
    }

    public T getObjects(int pos) {
        return (T) objects[pos];
    }
}

class Student extends Person {
}

class TestGeneric<T extends Number> {
}

class TestGeneric2<T extends Person> {
}


class Person implements Comparable<Person> {
    @Override
    public int compareTo(Person o) {
        return 0;
    }
}

//写一个泛型类，求一个数组中的最大值  T一定是引用数据类型，最终被擦除为了Object类型
//T类型 一定是可以比较的，问题：怎么能够约束这个T，一定是可以比较大小的？ T extends Comparable<T>
//T一定是实现了 Comparable接口的
class Alg<T extends Comparable<T>> {
    public T findMaxValue(T[] array) {
        T max = array[0];
        for (int i = 0; i < array.length; i++) {
            if (max.compareTo(array[i]) < 0) {
                max = array[i];
            }
        }
        return max;
    }
}

class Alg2 {
    public <T extends Comparable<T>> T findMaxValue(T[] array) {
        T max = array[0];
        for (int i = 0; i < array.length; i++) {
            if (max.compareTo(array[i]) < 0) {
                max = array[i];
            }
        }
        return max;
    }
}


class Alg3 {
    public static <T extends Comparable<T>> T findMaxValue(T[] array) {
        T max = array[0];
        for (int i = 0; i < array.length; i++) {
            if (max.compareTo(array[i]) < 0) {
                max = array[i];
            }
        }
        return max;
    }
}


public class Test3 {
    public static void main(String[] args) {
        Integer[] integers3 = {1, 2, 3, 4, 5, 6, 7, 8};
        System.out.println(Alg3.<Integer>findMaxValue(integers3));
        System.out.println("====================");


        Alg2 alg2 = new Alg2();
        Integer[] integers2 = {1, 2, 3, 4, 5, 6, 7};
        //类型推到：根据实参传值，来推导此时的类型
        Integer ret2 = alg2.findMaxValue(integers2);
        //Integer ret2 = alg2.<Integer>findMaxValue(integers2);
        System.out.println(ret2);
        System.out.println("====================");


        Alg<Integer> alg = new Alg<>();
        Integer[] integers = {1, 2, 3, 4, 5, 6};
        Integer ret = alg.findMaxValue(integers);
        System.out.println(ret);

        //Alg<Person> alg2 = new Alg<>();
    }


    public static void main2(String[] args) {
        TestGeneric<Number> testGeneric = new TestGeneric<>();
        TestGeneric<Integer> testGeneric2 = new TestGeneric<>();
        TestGeneric<Double> testGeneric3 = new TestGeneric<>();
        //String 不是Number的子类
        //TestGeneric<String> testGeneric4 = new TestGeneric<>();

        TestGeneric2<Person> testGeneric22 = new TestGeneric2<>();
        TestGeneric2<Student> testGeneric23 = new TestGeneric2<>();
        //Integer 不是Person的子类
        //TestGeneric2<Integer> testGeneric24 = new TestGeneric2<>();
    }


    public static void main1(String[] args) {
        MyArray2<Integer> myArray2 = new MyArray2<>();
        //编译的时候，帮助我们进行类型的检查，取数据不需要进行类型转换(相同)
        myArray2.setObjects(0, 123);//
        myArray2.setObjects(1, 456);
        myArray2.setObjects(2, 666);

        int a = myArray2.getObjects(2);
        double bb = myArray2.getObjects(1);//
        System.out.println(a);
    }
}
